# 图片处理的类

from datetime import datetime
import logging  
logging.basicConfig(level=logging.INFO)  


def calc_age_from_id(id_number):  
    # 假设id_number是一个18位的身份证号码  
    if len(id_number) != 18:  
        logging.warn(f"格式错误： 身份证号码{id_number}长度应为18位")  
        return 0
      
    # 提取出生年月日（7-14位）  
    birth_year = int(id_number[6:10])  
    birth_month = int(id_number[10:12])  
    birth_day = int(id_number[12:14])  
      
    # 创建出生日期对象  
    birth_date = datetime(birth_year, birth_month, birth_day)  
      
    # 获取今天的日期  
    today = datetime.today()  
      
    # 计算年龄（年份差）并考虑是否过了生日  
    if today.month < birth_month or (today.month == birth_month and today.day < birth_day):  
        age = today.year - birth_year - 1  
    else:  
        age = today.year - birth_year  
      
    return age  
  
def get_gender_from_id(id_number):  
    # 检查身份证号码长度  
    if len(id_number) == 15:  
        # 15位身份证号码没有校验码，性别在第15位  
        gender_code = int(id_number[-1])  
        if gender_code % 2 == 0:  
            return '2'  
        else:  
            return '1'  
    elif len(id_number) == 18:  
        # 18位身份证号码，性别在第17位  
        gender_code = int(id_number[16])  
        if gender_code % 2 == 0:  
            return '2'  
        else:  
            return '1'  
    else:  
        logging.warn(f"格式错误：{id_number} 身份证号码长度应为15位或18位")  
    return ''

